package org.jeecg.modules.pbmp.reimbursementInfo.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.pbmp.reimbursementInfo.entity.ReimbursementInfo;
import org.jeecg.modules.pbmp.reimbursementInfo.mapper.ReimbursementInfoMapper;
import org.jeecg.modules.pbmp.reimbursementInfo.service.IReimbursementInfoService;
import org.jeecg.modules.pbmp.trafficStay.entity.TrafficStay;
import org.jeecg.modules.pbmp.trafficStay.service.ITrafficStayService;
import org.jeecg.modules.pbmp.travelAllowance.entity.TravelAllowance;
import org.jeecg.modules.pbmp.travelAllowance.service.ITravelAllowanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Description: 报销单管理
 * @Author: jeecg-boot
 * @Date:   2021-01-30
 * @Version: V1.0
 */
@Service
public class ReimbursementInfoServiceImpl extends ServiceImpl<ReimbursementInfoMapper, ReimbursementInfo> implements IReimbursementInfoService {

    @Autowired
    private ITrafficStayService trafficStayService;
    @Autowired
    private ITravelAllowanceService travelAllowanceService;

    @Override
    public Page<ReimbursementInfo> pageReimbursementList(Page<ReimbursementInfo> page, ReimbursementInfo reimbursementInfo) {
        List<ReimbursementInfo> l = baseMapper.pageReimbursementList(page,reimbursementInfo);
        return page.setRecords(l);
    }

    @Override
    public void saveMain(ReimbursementInfo reimbursementInfo, List<TrafficStay> trafficStayList, List<TravelAllowance> travelAllowanceList) {//        baseMapper.insert(reimbursementInfo);
        baseMapper.insert(reimbursementInfo);
        update(reimbursementInfo,trafficStayList,travelAllowanceList);
    }

    public void update(ReimbursementInfo reimbursementInfo, List<TrafficStay> trafficStayList, List<TravelAllowance> travelAllowanceList){
        //车费合计
        BigDecimal a = new BigDecimal(0);
        //住宿费合计
        BigDecimal b = new BigDecimal(0);
        //差旅费合计
        BigDecimal c = new BigDecimal(0);
        //其他费用合计
        BigDecimal d = new BigDecimal(0);
        //合计
        BigDecimal e = new BigDecimal(0);
        //总计
        BigDecimal f = new BigDecimal(0);
        for (int i = 0; i < trafficStayList.size(); i++) {
            TrafficStay trafficStay = trafficStayList.get(i);
            trafficStay.setReimbursementId(reimbursementInfo.getId());
            b = b.add(changValue(trafficStay.getStayAmount()));
            a = a.add(changValue(trafficStay.getInfoAmount()));
            a = a.add(changValue(trafficStay.getTicketsAmount()));
            a = a.add(changValue(trafficStay.getDestinedPlace()));
            a = a.add(changValue(trafficStay.getLeavePlace()));
            trafficStayService.save(trafficStay);
        }
        for (int i = 0; i < travelAllowanceList.size(); i++) {
            TravelAllowance travelAllowance = travelAllowanceList.get(i);
            travelAllowance.setReimbursementId(reimbursementInfo.getId());
            if(i<trafficStayList.size()){
                travelAllowance.setStayId(trafficStayList.get(i).getId());
            }else{
                TrafficStay trafficStay = new TrafficStay();
                trafficStay.setReimbursementId(reimbursementInfo.getId());
                trafficStayService.save(trafficStay);
                travelAllowance.setStayId(trafficStay.getId());
            }
            String travelNum = travelAllowance.getTravelNum();
            String travelStandard = travelAllowance.getTravelStandard();
            c = c.add(new BigDecimal(travelNum).multiply(new BigDecimal(travelStandard)));
            travelAllowanceService.save(travelAllowance);
        }
        BigDecimal businessAmount = reimbursementInfo.getBusinessAmount();
        businessAmount = changValue(businessAmount);
        BigDecimal carAmount = reimbursementInfo.getCarAmount();
        carAmount = changValue(carAmount);
        BigDecimal leaseAmount = reimbursementInfo.getLeaseAmount();
        leaseAmount = changValue(leaseAmount);
        BigDecimal lowAmount = reimbursementInfo.getLowAmount();
        lowAmount = changValue(lowAmount);
        BigDecimal logisticsAmount = reimbursementInfo.getLogisticsAmount();
        logisticsAmount = changValue(logisticsAmount);
        BigDecimal correspondAmount = reimbursementInfo.getCorrespondAmount();
        correspondAmount = changValue(correspondAmount);
        BigDecimal materielAmount = reimbursementInfo.getMaterielAmount();
        materielAmount = changValue(materielAmount);
        BigDecimal officeAmount = reimbursementInfo.getOfficeAmount();
        officeAmount = changValue(officeAmount);
        BigDecimal otherAmount = reimbursementInfo.getOtherAmount();
        otherAmount = changValue(otherAmount);
        BigDecimal propertyAmount = reimbursementInfo.getPropertyAmount();
        propertyAmount = changValue(propertyAmount);
        BigDecimal trafficAmount = reimbursementInfo.getTrafficAmount();
        trafficAmount = changValue(trafficAmount);
        d = d.add(businessAmount).add(carAmount).add(leaseAmount).add(lowAmount)
                .add(logisticsAmount).add(correspondAmount).add(materielAmount).add(officeAmount)
                .add(otherAmount).add(propertyAmount).add(trafficAmount);
        e = e.add(a).add(b).add(c);
        f = f.add(e).add(d);
        reimbursementInfo.setAAmount(a);
        reimbursementInfo.setBAmount(b);
        reimbursementInfo.setCAmount(c);
        reimbursementInfo.setEAmount(e);
        reimbursementInfo.setFAmount(f);
        baseMapper.updateById(reimbursementInfo);
    }

    public BigDecimal changValue(BigDecimal s){
        if(s==null||"".equals(s)){
            s = new BigDecimal(0);
        }
        return s;
    }

    @Override
    public void updateMain(ReimbursementInfo reimbursementInfo, List<TrafficStay> trafficStayList, List<TravelAllowance> travelAllowanceList) {
        deleteMain(reimbursementInfo.getId());
        update(reimbursementInfo,trafficStayList,travelAllowanceList);
    }

    @Override
    public void deleteMain(String id) {
        baseMapper.deleteTrafficById(id);
        baseMapper.deleteTravelById(id);
    }
}
